#include <algorithm>
#include <chrono>
#include <cassert>
#include <pthread.h>
#include <err.h>

namespace tue
{
    struct  defer_lock_t {};
    struct  try_to_lock_t {};
    struct  adopt_lock_t {};
    constexpr defer_lock_t  defer_lock  = defer_lock_t();
    constexpr try_to_lock_t try_to_lock = try_to_lock_t();
    constexpr adopt_lock_t  adopt_lock  = adopt_lock_t();

    class mutex
    {
        pthread_mutex_t __m_;

    public:
        mutex() noexcept {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;}
        ~mutex();

    private:
        mutex(const mutex&);// = delete;
        mutex& operator=(const mutex&);// = delete;

    public:
        void lock();
        bool try_lock() noexcept;
        void unlock() noexcept;

        /* typedef pthread_mutex_t* native_handle_type; */
        /* _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;} */
    };

    mutex::~mutex()
    {
        pthread_mutex_destroy(&__m_);
    }

    void mutex::lock()
    {
        int ec = pthread_mutex_lock(&__m_);
        if (ec)
            err(ec, "mutex lock failed");
    }

    bool mutex::try_lock() noexcept
    {
        return pthread_mutex_trylock(&__m_) == 0;
    }

    void mutex::unlock() noexcept
    {
        int ec = pthread_mutex_unlock(&__m_);
        (void)ec;
        assert(ec == 0);
    }


    template <class _Mutex>
    class lock_guard
    {
    public:
        typedef _Mutex mutex_type;
    private:
        mutex_type& __m_;
    public:
        explicit lock_guard(mutex_type& __m)
            : __m_(__m) {__m_.lock();}
        lock_guard(mutex_type& __m, adopt_lock_t)
            : __m_(__m) {}
        ~lock_guard() {__m_.unlock();}
    private:
        lock_guard(lock_guard const&);// = delete;
        lock_guard& operator=(lock_guard const&);// = delete;
    };

    template <class _Mutex>
    class unique_lock
    {
    public:
        typedef _Mutex mutex_type;
    private:
        mutex_type* __m_;
        bool __owns_;

    public:
        unique_lock() noexcept : __m_(nullptr), __owns_(false) {}
        explicit unique_lock(mutex_type& __m)
            : __m_(&__m), __owns_(true) {__m_->lock();}
        unique_lock(mutex_type& __m, defer_lock_t) noexcept
            : __m_(&__m), __owns_(false) {}
        unique_lock(mutex_type& __m, try_to_lock_t)    //偏特化
            : __m_(&__m), __owns_(__m.try_lock()) {}
        unique_lock(mutex_type& __m, adopt_lock_t)     //偏特化
            : __m_(&__m), __owns_(true) {}
        template <class _Clock, class _Duration>
            unique_lock(mutex_type& __m, const std::chrono::time_point<_Clock, _Duration>& __t)
                : __m_(&__m), __owns_(__m.try_lock_until(__t)) {}
        template <class _Rep, class _Period>
            unique_lock(mutex_type& __m, const std::chrono::duration<_Rep, _Period>& __d)
                : __m_(&__m), __owns_(__m.try_lock_for(__d)) {}
        ~unique_lock()
        {
            if (__owns_)
                __m_->unlock();
        }

    private:
        unique_lock(unique_lock const&); // = delete;
        unique_lock& operator=(unique_lock const&); // = delete;

    public:
        unique_lock(unique_lock&& __u) noexcept
            : __m_(__u.__m_), __owns_(__u.__owns_)
            {__u.__m_ = nullptr; __u.__owns_ = false;}
        unique_lock& operator=(unique_lock&& __u) noexcept
            {
                if (__owns_)
                    __m_->unlock();
                __m_ = __u.__m_;
                __owns_ = __u.__owns_;
                __u.__m_ = nullptr;
                __u.__owns_ = false;
                return *this;
            }

        void lock();
        bool try_lock();

        template <class _Rep, class _Period>
        bool try_lock_for(const std::chrono::duration<_Rep, _Period>& __d);
        template <class _Clock, class _Duration>
        bool try_lock_until(const std::chrono::time_point<_Clock, _Duration>& __t);

        void unlock();
        void swap(unique_lock& __u) noexcept
        {
            std::swap(__m_, __u.__m_);
            std::swap(__owns_, __u.__owns_);
        }
        mutex_type* release() noexcept
        {
            mutex_type* __m = __m_;
            __m_ = nullptr;
            __owns_ = false;
            return __m;
        }
        bool owns_lock() const noexcept {return __owns_;}
        operator bool () const noexcept {return __owns_;}
        mutex_type* mutex() const noexcept {return __m_;}
    };
}

